package com.heu.blood.application.controller;

import com.heu.blood.BloodApplication;
import com.heu.blood.application.entity.BloodApplicationEntity;
import com.heu.blood.application.exception.ApplicationExceptionEnum;
import com.heu.blood.application.service.BloodApplicationService;
import com.heu.blood.application.vo.*;
import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.globalExceptionHandler.MyException;
import com.heu.blood.match.entity.BloodInformationEntity;
import com.heu.blood.send.vo.BloodInformationAndSendVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;




/**
 *
 *
 * @author lyj
 * @email lyj@heu.com
 * @date 2024-07-15 21:14:57
 */
@RestController
@RequestMapping("application/bloodapplication")
public class BloodApplicationController {
    @Autowired
    private BloodApplicationService bloodApplicationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "申请单模块", operate = "分页查询申请单信息")
    public R list(@RequestBody ApplicationSelectVo applicationSelectVo){
        PageUtils page = bloodApplicationService.queryPage(applicationSelectVo);
        return R.ok().put("page", page);
    }


    /**
     * 根据applicationId查询本条详细信息
     */
    @RequestMapping("/info/{applicationId}")
    @ControllerLogAnnotation(module = "申请单模块", operate = "根据applicationId查询本条详细信息")
    public R applicationInfo(@PathVariable("applicationId") String applicationId){
        BloodApplicationVo bloodApplication = bloodApplicationService.applicationInfo(applicationId);

        return R.ok().put("applicationInfo", bloodApplication);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "申请单模块", operate = "保存申请单信息")
    public R save(@RequestBody BloodApplicationEntity bloodApplication){
        bloodApplicationService.save(bloodApplication);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "申请单模块", operate = "修改申请单信息")
    public R update(@RequestBody BloodApplicationEntity bloodApplication){
        bloodApplicationService.updateById(bloodApplication);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "申请单模块", operate = "批量逻辑删除申请单信息")
    public R delete(@RequestBody Long[] ids){
        bloodApplicationService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 按照ID删除所有对应的申请单和申请单详情
     */
    @RequestMapping("/removeApplicationAndItemsByIds")
    @ControllerLogAnnotation(module = "申请单模块", operate = "按照ID删除所有对应的申请单和申请单详情")
    public R removeApplicationAndItemsByIds(@RequestBody List<BloodApplicationEntity> bloodApplications){
        int flag = bloodApplicationService.removeApplicationAndItemsByIds(bloodApplications);
        return R.ok();
    }

    /**
     * 保存输血申请单（这个是实际用的）
     * @param bloodApplication
     * @return
     */
    @RequestMapping("/saveApplication")
    @ControllerLogAnnotation(module = "申请单模块", operate = "保存输血申请单")
    public R saveApplication(@RequestBody BloodApplicationVo bloodApplication){
        int flag = bloodApplicationService.saveApplication(bloodApplication);
        if(flag == 0){
            return R.ok();
        }else if(flag == 1){
            throw new MyException(ApplicationExceptionEnum.ILLEGAL_PARAMS);
        }
        return R.ok();
    }

    /**
     * 修改或者更新用血申请单（这个是实际用的）
     * @param bloodApplication
     * @return
     */
    @RequestMapping("/updateApplication")
    @ControllerLogAnnotation(module = "申请单模块", operate = "修改或者更新用血申请单")
    public R updateApplication(@RequestBody BloodApplicationVo bloodApplication){
        int flag = bloodApplicationService.updateApplication(bloodApplication);
        if(flag == 0){
            return R.ok();
        }else if(flag == 1){
            throw new MyException(ApplicationExceptionEnum.ILLEGAL_PARAMS);
        }
        return R.ok();
    }

    /**
     * 查询输血流程中的列表（就是流程下面的那个单子）
     * @param applicationSelectVo
     * @return
     */
    @RequestMapping("/centFlowList")
    @ControllerLogAnnotation(module = "申请单模块", operate = "查询输血流程中的列表")
    public R centFlowList(@RequestBody ApplicationSelectVo applicationSelectVo){
        CentFlowResultVo centFlowResultVO = bloodApplicationService.centFlowList(applicationSelectVo);
        return R.ok().put("data",centFlowResultVO);
    }

    /**
     * 查询输血申请单详情
     * @param bloodApplicationVo
     * @return
     */
    @RequestMapping("/getApplicationInfo")
    @ControllerLogAnnotation(module = "申请单模块", operate = "查询输血申请单详情")
    public R getApplicationInfo(@RequestBody BloodApplicationVo bloodApplicationVo){
        BloodApplicationVo res = bloodApplicationService.getApplicationInfo(bloodApplicationVo);
        return R.ok().put("data",res);
    }

    /**
     * 批量把申请单对应的所有的审批状态设置为审批完成
     * @param bloodApplicationVo
     * @return
     */
    @RequestMapping("/examineAllApplication")
    @ControllerLogAnnotation(module = "申请单模块", operate = "批量把申请单对应的所有的审批状态设置为审批完成")
    public R examineAllApplication(@RequestBody BloodApplicationVo bloodApplicationVo){
        int flag = bloodApplicationService.examineAllApplication(bloodApplicationVo);
        return R.ok();
    }

    /**
     * 根据申请单号信息查询所有的流转状态
     * @param bloodApplicationVo
     * @return
     */
    @RequestMapping("/getAllCenterFlowStatus")
    @ControllerLogAnnotation(module = "申请单模块", operate = "根据申请单号信息查询所有的流转状态")
    public R getAllCenterFlowStatus(@RequestBody BloodApplicationVo bloodApplicationVo){
        CenterFlowStatusResultVo centerFlowStatusResultVo = bloodApplicationService.getAllCenterFlowStatus(bloodApplicationVo);
        return R.ok().put("data",centerFlowStatusResultVo);
    }

    /**
     * 根据申请单号信息查询所有的流转状态，按照当前申请项id来获取
     * @param bloodApplicationVo
     * @return
     */
    @RequestMapping("/getAllCenterFlowStatusWithItemId")
    @ControllerLogAnnotation(module = "申请单模块", operate = "根据申请单号信息查询所有的流转状态，按照当前申请项id来获取")
    public R getAllCenterFlowStatusWithItemId(@RequestBody BloodApplicationVo bloodApplicationVo){
        CenterFlowStatusResultVo centerFlowStatusResultVo = bloodApplicationService.getAllCenterFlowStatusWithItemId(bloodApplicationVo);
        return R.ok().put("data",centerFlowStatusResultVo);
    }

    /**
     * 根据申请单号获取申请单的生命周期
     * @param applicationId
     * @return
     */
    @RequestMapping("/getBloodApplicationHistory")
    @ControllerLogAnnotation(module = "申请单模块", operate = "根据申请单号获取申请单的生命周期")
    public R getBloodApplicationHistory(String applicationId){
        BloodApplicationHistoryVo bloodApplicationHistory = bloodApplicationService.getBloodApplicationHistory(applicationId);
        return R.ok().put("data",bloodApplicationHistory);
    }

    /**
     * 根据申请单号获取申请单的生命周期
     * @param applicationId
     * @return
     */
    @RequestMapping("/setApplicationPrint")
    @ControllerLogAnnotation(module = "申请单模块", operate = "更改申请单打印状态")
    public R setApplicationPrint(String applicationId){
        int flag = bloodApplicationService.setApplicationPrint(applicationId);
        if(flag == 0){
            return R.ok();
        }else if(flag == 1){
            throw new MyException(ApplicationExceptionEnum.ILLEGAL_PARAMS);
        }
        return R.ok();
    }

    /**
     * 根据申请单号查找到所有已发血的血液
     * @param applicationId
     * @return
     */
    @RequestMapping("/getBloodListUsedWithApplicationId")
    @ControllerLogAnnotation(module = "申请单模块", operate = "根据申请单号查找到所有已发血的血液")
    public R getBloodListUsedWithApplicationId(String applicationId){
        List<BloodInformationAndSendVo> res = bloodApplicationService.getBloodListUsedWithApplicationId(applicationId);
        return R.ok().put("data",res);
    }

    @RequestMapping("/getInfoOrderByIfprintAndTime")
    @ControllerLogAnnotation(module = "申请单模块", operate = "增加打印排序规则后分页查询申请单信息")
    public R getInfoOrderByIfprintAndTime(@RequestBody ApplicationSelectVo applicationSelectVo){
        PageUtils page = bloodApplicationService.getInfoOrderByIfprintAndTime(applicationSelectVo);
        return R.ok().put("page", page);
    }

    @RequestMapping("/reapplyApplication")
    @ControllerLogAnnotation(module = "申请单模块", operate = "上一份申请单被退回后重新申请")
    public R reapplyApplication(@RequestBody BloodApplicationVo bloodApplication){
        int flag = bloodApplicationService.reapplyApplication(bloodApplication);
        if(flag == 0){
            return R.ok();
        }else if(flag == 1){
            throw new MyException(ApplicationExceptionEnum.ILLEGAL_PARAMS);
        }
        return R.ok();
    }
}
